using System;
using System.IO;
using System.Net.Http;
using System.Net.Sockets;
using Client;
using Greet;
using Grpc.Net.Client;

var socketPath = Path.Combine(Path.GetTempPath(), "grpc-transporter.tmp");

using var channel = GrpcChannel.ForAddress("http://localhost", new GrpcChannelOptions
{
    HttpHandler = CreateHttpHandler(socketPath)
});
var client = new Greeter.GreeterClient(channel);

var reply = await client.SayHelloAsync(new HelloRequest { Name = "GreeterClient" });
Console.WriteLine("Greeting: " + reply.Message);

Console.ReadKey();

static SocketsHttpHandler CreateHttpHandler(string socketPath)
{
    var udsEndPoint = new UnixDomainSocketEndPoint(socketPath);
    var connectionFactory = new UnixDomainSocketConnectionFactory(udsEndPoint);
    var socketsHttpHandler = new SocketsHttpHandler
    {
        ConnectCallback = connectionFactory.ConnectAsync
    };

    return socketsHttpHandler;
}
